﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class EducationUpdateForm : Form
    {
        #region Fields
        private int subSelected = 0;
        private int insSelected = 0;
        I_UAC_EducationalBackground edicationlist = new UAC_EducationalBackground();
        I_UAC_InstitutionBackground instatuition;
        #endregion

        #region Constructor
        public EducationUpdateForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Singelton
        internal static List<I_UAC_InstitutionBackground> Show(List<I_UAC_InstitutionBackground> eduList)
        {
            EducationUpdateForm ed = new EducationUpdateForm();

            ed.edicationlist.Institution = eduList;
            ed.cbxNQFLevel.DataSource = Enum.GetValues(typeof(UAC_Grade));
            ed.SetLists(eduList);
            ed.ShowDialog();
            return eduList;
        }
        #endregion

        #region Lists
        private void lbxInsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbxInsList.SelectedIndex > -1)
            {
                ClearSub();
                insSelected = lbxInsList.SelectedIndex;
                foreach (I_UAC_InstitutionSubjectResult sub in edicationlist.Institution[insSelected].InstitutionSubjects)
                {
                    instatuition = edicationlist.Institution[insSelected];
                    lbxSubList.Items.Add(sub.SubjectName);
                }
            }
        }

        private void lbxSubList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbxSubList.SelectedIndex > -1)
            {
                subSelected = lbxSubList.SelectedIndex;
                DisplayDetails();
            }
        }
        #endregion

        #region Set Form Methods
        private void ClearSub()
        {
            lbxSubList.Items.Clear();
        }
        private void DisplayDetails()
        {
            txtSubjectName.Text = instatuition.InstitutionSubjects[subSelected].SubjectName;
            txtCode.Text = instatuition.InstitutionSubjects[subSelected].SubjectCode;
            cbxNQFLevel.Text = instatuition.InstitutionSubjects[subSelected].NQFLevel.ToString();
            txtResult.Text = instatuition.InstitutionSubjects[subSelected].Result;
        }
        private void SetLists(List<I_UAC_InstitutionBackground> elist)
        {
            lbxInsList.Items.Clear();
            lbxSubList.Items.Clear();
            foreach (I_UAC_InstitutionBackground Ins in elist)
            {
                lbxInsList.Items.Add(Ins.InstitutionName);
            }
        }
        #endregion

        #region Buttons
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            instatuition.InstitutionSubjects[subSelected].SubjectName = txtSubjectName.Text;
            instatuition.InstitutionSubjects[subSelected].SubjectCode = txtCode.Text;
            instatuition.InstitutionSubjects[subSelected].NQFLevel = (UAC_Grade)cbxNQFLevel.SelectedItem;
            instatuition.InstitutionSubjects[subSelected].Result = txtResult.Text;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            I_UAC_InstitutionSubjectResult newSubject = new UAC_InstitutionSubjectResult(txtSubjectName.Text, txtCode.Text, (UAC_Grade)cbxNQFLevel.SelectedItem, txtResult.Text);
            instatuition.InstitutionSubjects.Add(newSubject);
        }

        private void btnRemoveIns_Click(object sender, EventArgs e)
        {
            edicationlist.Institution.RemoveAt(insSelected);
            MessageBox.Show(this, "Institution Removed");
            SetLists(edicationlist.Institution);
        }

        private void btnRemoveSub_Click(object sender, EventArgs e)
        {
            edicationlist.Institution[insSelected].InstitutionSubjects.RemoveAt(subSelected);
            MessageBox.Show(this, "Subject Removed");
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
